// pages/home/home.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchKey: "",
    feature: [
      {
        title: "文本工具",
        show: true,
        grids: [
          {
            title: "OCR",
            keywords: ['ocr', 'OCR', '文字识别'],
            icon: "center-focus-strong",
            page: "/pages/text_ocr/ocr",
            show: true,
          },
          {
            title: "二维码生成",
            keywords: ['二维码', 'qrcode'],
            icon: "barcode",
            page: "/pages/text_qrcode/qrcode",
            show: true,
          },
        ]
      }
    ]
  },

  onIconTap: function (event: WechatMiniprogram.CustomEvent) {
    let page:string = event.target.dataset['page']
  
    wx.navigateTo({
      url: page,
    })
  },

  onSearchChange(e: WechatMiniprogram.CustomEvent) {
    let { value } = e.detail
    this.data.searchKey = value
  },

  onSearchClear() {
    this.data.searchKey = ''
    this.onSearch()
  },

  onSearch() {
    console.log(this.data.searchKey)
    const key = this.data.searchKey
    const features = this.data.feature

    features.forEach(feature => {
      if (!feature.grids || feature.grids.length <= 0) {
        return
      }

      let grids: any[] = []
      feature.grids.forEach(grid => {
        if (key == '' || (!!grid.keywords && grid.keywords.includes(key))) {
          grids.push(grid)
          grid.show = true
        } else {
          grid.show = false
        }
      })
      if (grids.length == 0) {
        feature.show = false
      } else {
        feature.show = true
      }
    })
    this.setData({
      feature: features,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})